package dk.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import dk.Utils.COSUtils;
import dk.Utils.POIUtils;
import dk.Utils.SpringContextUtils;
import dk.beans.Examlist;
import dk.beans.EasyPoiExcelBean.ExamBean;

import dk.mapper.examlistmapper;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Multithreading  implements Runnable{
	
	Integer TotalScore;
	Integer	OptionNum;
	Integer ShortAnswerNum;
	Integer MaxDiffculty;
	Integer minDidffculty;
	String UUid;
	Integer OptionExamListCount;
	Integer ShortAnswerExamListCount;

	@Override
	public void run() {
		QueryWrapper<Examlist> qw11 = new QueryWrapper<Examlist>();
		qw11.orderByDesc("id");
		examlistmapper examlistmapper =(examlistmapper)SpringContextUtils.getBeanByClass(examlistmapper.class);
	
		//获取到列表
		List<Examlist> examlist = examlistmapper.selectList(qw11);
		
		
		//data.setData("https://misaka917-1301164487.cos.ap-nanjing.myqcloud.com/"+uuid+".xls");
		//获取到最大id
		//data.setData(examlist.get(0).getId());
		//获取到第一个元素 
		Examlist examlist1 = examlist.get(0);
		//选择随机数范围
				Random Optionrandom = new Random();
				//简答随机数范围
				Random ShortAnswerrandom = new Random();
				//声明接受用set 选择
				Set<Integer> set = new HashSet<Integer>();
				//生成接受用set 简答
				Set<Integer> set1 = new HashSet<Integer>();
				List<Examlist> reslist = new ArrayList<Examlist>();
				while(set.size() < OptionNum) {
					QueryWrapper<Examlist> qw = new QueryWrapper<Examlist>();
					int id = Optionrandom.nextInt(examlist1.getId());
					//优化 判断是否在set里
					if(!set.contains(id)) {
						int count = examlistmapper.selectCount(qw.eq("id",id).eq("type", 0));
						if(count!=0) {
							set.add(id);
							//把答案置空
							Examlist examlist2 = examlistmapper.selectOne(qw.eq("id",id));
							examlist2.setAnswer("  ");
							reslist.add(examlist2);
						}
						
					}
					
					System.out.println("单选id表长度为"+set.size());
				}
				while(set1.size()<ShortAnswerNum) {
					QueryWrapper<Examlist> qw = new QueryWrapper<Examlist>();
					int id = ShortAnswerrandom.nextInt(examlist1.getId());
					if(!set1.contains(id)) {
						int count = examlistmapper.selectCount(qw.eq("id",id).eq("type", 1));
						if(count!=0) {
							set1.add(id);
							//把答案置空
							Examlist examlist2 = examlistmapper.selectOne(qw.eq("id",id));
							examlist2.setAnswer("  ");
							reslist.add(examlist2);
						}
						
					}
					
					System.out.println("简答id表长度为"+set1.size());
				}
				System.out.println("生成题目成功,准备写入excel");
				//写入到excel里面之前需要进行pojo转换
				
				List<ExamBean> exambeanlist = new ArrayList<ExamBean>();
				for(Examlist list : reslist) {
					ExamBean exam = new ExamBean();
					exam.setAnswer("  ");
					exam.setTitle(list.getTitle());
					exam.setOptiona(list.getOptiona());
					exam.setOptionb(list.getOptionb());
					exam.setOptionc(list.getOptionc());
					exam.setOptiond(list.getOptiond());
					exam.setId(list.getId());
					exambeanlist.add(exam);
					
				}
				String path = "C:\\Users\\34227\\Desktop\\实习笔记\\easypoi文件\\"+UUid+".xls";
				String CloudPath ="/examlist/"+UUid+".xls"; 
				try {
					POIUtils.WriteToFile(path, "请填写信息不要更改id,试卷标识码为"+UUid, "试卷一", ExamBean.class, exambeanlist);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				COSUtils.uploadFileByPath(path, CloudPath);
				
		
		
		
	}

}
